﻿
using Grpc.Client;
using Grpc.Core;
using Sigma.Admin.Protos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sigma.Admin.Client.GrpcService
{
    public class ChartService:IDisposable
    {
        private AsyncServerStreamingCall<LineXY> _call;

        public void Dispose()
        {
            _call?.Dispose();
        }

        public async IAsyncEnumerable<LineXY> GetChartAsync()
        {
            //var headers = new Metadata();
            //headers.Add("Authorization", $"Bearer {token}");
            var _chartClient = new GrpcServiceProvider().GetChartClient();
            //添加认证头
            var headers = new Metadata();
            headers.Add("Authorization", $"Bearer {AppSession.Instance.Token}");

            using (_call = _chartClient.GetOxy(new NullParma(),headers))
            {
                await foreach (var response in _call.ResponseStream.ReadAllAsync())
                {
                    yield return response;
                }
            }
        }

        public Task SayHello()
        {
            var _chartClient = new GrpcServiceProvider().GetChartClient();

            var res = _chartClient.SayHello(new TestMsg { Msg = "hellow,this is client" });
            return Task.CompletedTask;
        }

        public void StopStream()
        {
            _call?.Dispose();
        }
    }
}
